<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Delete a association user account.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.4
 * 
 * 0.2)  Inherited from the DeleteAction.
 * 0.3)  Adds the 'Association_Mark' feature, but not so strict.
 * 0.4)  Adds the deletion of the avatar.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

//Requirements.
require_once INSTALLDIR . '/lib/slh_deleteaction.php';

require_once INSTALLDIR . '/classes/User.php';
require_once INSTALLDIR . '/classes/Association.php';
require_once INSTALLDIR . '/classes/Association_Mark.php';
require_once INSTALLDIR . '/classes/Avatar.php';


class DeleteassociationuserAction extends DeleteAction
{
	/**
	 * Constructor.
	 * To pass some variables to the parent. 
	 */
	function __construct()
	{
		parent::__construct('社团用户',
							'guser_id',
							'deleteassociationuser');
	}
	
	function title()
	{
		return ('Delete an association user.');
	}
	
	/**
	 * Check the user and rights.
	 */
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_ASSOCITION)) {
			return false;
		}

		return parent::prepare($args);
	}
	
// Overrided ends //////////////////////////////////////////////////////////////	
	/**
	 * The default returnto while the 'returnto' is not given.
	 * Overrided.
	 */
	function returnToDefault()
	{
		slh_redirect(slh_action2url('showassociationuser'), 303);
	}
	
	/**
	 * Actually delete a micro news.
	 * Overriede.
	 */
	function handleDelete()
	{
		$user = User::staticGet('id', $this->itemid);
		$association = Association::staticGet('user_id', $this->itemid);
		$association_mark = Association_Mark::staticGet('user_id', $this->itemid);
		
		if (empty($user)) {
			$this->clientError('找不到这个账户呃.');
		} else if (empty($association)) {
			$this->clientError('找不到这个社团用户呃.');
		}
			
		$user->query('BEGIN');
		
		$u = $user->delete();
		$a = $association->delete();
		$ak = $association_mark->delete();
		
		if ( !$u || !$a) {
			// FIXME: Logs.
			$user->query('ROLLBACK');
			$this->clientError('删除社团用户出错了,亲!');
		}
		
		$user->query('COMMIT');
		
		// Delete avatars.
		$query = 'SELECT * from `Avatar` where ' .
				 '`user_id` = ' . $this->itemid;
		$avatar = new Avatar();
		$avatar->query($query);
		while ($avatar->fetch()) {
			$avatar->delete();
		}
	}
	
}

?>